package com.zghhost.codedemo.other;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.*;

/**
 * @author guohua.zhang@zhaopin.com.cn
 * @Date 2019/6/27 10:09
 */
public class CountNewOrgNum {
    private static LinkedBlockingQueue<Runnable> linkedBlockingQueue = new LinkedBlockingQueue<Runnable>(1024);
    private static ExecutorService pool = new ThreadPoolExecutor(20, 20,
            0L, TimeUnit.MILLISECONDS,
            linkedBlockingQueue, Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());

    public static void main(String[] args)throws Exception{
        File file = new File("/Users/wow/Downloads/job0703/comid.csv");
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));

        String pulsarUrl = "pulsar://172.17.5.175:6650";
        String topic = "persistent://middle/campus-position-merge/org.dp.add";

        StringBuffer sb = new StringBuffer();
        int i=1;

        String line = null;
        while((line = br.readLine()) != null){
            if(line.contains("job_position_number")){
                continue;
            }
            String num = line;

            if(i == 30){
                while (linkedBlockingQueue.size() == 1024){

                }
                final String ids = sb.toString();
                pool.execute(()->{
                    try{
                        URL u = new URL("http://localhost:8081/orgrelease/customPulsarMsg?pulsarUrl="+pulsarUrl+"&topic="+topic+"&msg="+ids);
                        HttpURLConnection conn = (HttpURLConnection) u.openConnection();
                        System.out.println(conn.getResponseCode());
                        conn.disconnect();
                    }catch (Exception e){
                        e.printStackTrace();
                        System.out.println(500);
                    }
                });

                sb = new StringBuffer();
                i=1;
            }

            if(sb.length() > 0){
                sb.append("-~-");
            }

            sb.append("{\"data\":{\"orgId\":"+num+"}}");

            i++;
        }

        pool.shutdown();
        while (true){
            Thread.sleep(500);
            if(pool.isTerminated()){
                System.exit(-1);
            }
        }
    }
}
